package com.briup.pts.web.controller;

import com.briup.pts.config.response.Result;
import com.briup.pts.domain.bean.FenceHouse;
import com.briup.pts.domain.vo.FenceHouseWithChildren;
import com.briup.pts.service.FenceHouseService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 25095
 * @description
 * @data 2024/11/27-11:07
 */
@Slf4j
@Tag(name = "栏舍监管")
@RestController
@Validated
@RequiredArgsConstructor
@RequestMapping("/fenceHouse")
public class FenceHouseController {
    private final FenceHouseService service;

    @Operation(summary = "分页查询栏舍信息")
    @GetMapping("/query")
    public Result<?> query(String fhName,
                           @NotNull(message = "分页参数不能为空")
                           @Min(value = 1L, message = "当前页面不能小于1") Integer pageNum,
                           @Min(value = 1L, message = "页面大小不能小于1")
                           @NotNull(message = "分页参数不能为空")
                           Integer pageSize) {
        log.info("当前页码:{},页面大小:{}", pageNum, pageSize);
        PageInfo<FenceHouse> pageInfo = service
                .pageQueryByCondition(pageNum, pageSize, fhName);
        return Result.success(pageInfo)
                .apply("获取成功");
    }

    @Operation(summary = "根据栏舍编号查询栏舍信息以及栏圈信息")
    @GetMapping("/selectById")
    public Result<?> selectById(String fhId) {
        FenceHouseWithChildren fenceHouseWithChildren = service.selectById(fhId);
        return Result.success(fenceHouseWithChildren)
                .apply("获取成功");
    }

    @Operation(summary = "新增或修改栏舍")
    @PostMapping("/saveOrUpdate")
    public Result<?> saveOrUpdate(@Valid @RequestBody FenceHouse fh) {
        service.saveOrUpdate(fh);
        return Result.success(null).apply("操作成功");
    }

    @Operation(summary = "根据id删除栏舍信息")
    @DeleteMapping("/deleteById/{hId}")
    public Result<?> deleteById(@NotBlank(message = "id必须存在")
                                @PathVariable String hId) {
        service.deleteById(hId);
        return Result.success(null).apply("删除成功");
    }

    @Operation(summary = "批量删除栏舍")
    @DeleteMapping("/deleteByIdAll")
    public Result<?> deleteByIdAll(@NotEmpty(message = "id必须存在")
                                   @RequestBody List<String> ids) {
        service.deleteByIdAll(ids);
        return Result.success(null).apply("批量删除成功");
    }
}
